import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import {
  getPendingNotes,
  auditNote,
  getPendingReviews,
  auditReview,
} from '@/api/audit'

const initialState = {
  noteList: [],
  reviewList: [],
  noteTotal: 0,
  reviewTotal: 0,
  loading: false,
  error: null,
}

// 获取待审核笔记列表
export const fetchPendingNotes = createAsyncThunk(
  'audit/fetchPendingNotes',
  async ({ page = 1, size = 10 }, { rejectWithValue }) => {
    try {
      const data = await getPendingNotes(page, size)
      return data
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 审核笔记
export const reviewNote = createAsyncThunk(
  'audit/reviewNote',
  async ({ id, data }, { rejectWithValue }) => {
    try {
      const result = await auditNote(id, data)
      return { id, data: result }
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 获取待审核书评列表
export const fetchPendingReviews = createAsyncThunk(
  'audit/fetchPendingReviews',
  async ({ page = 1, size = 10 }, { rejectWithValue }) => {
    try {
      const data = await getPendingReviews(page, size)
      return data
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 审核书评
export const reviewBookReview = createAsyncThunk(
  'audit/reviewBookReview',
  async ({ id, data }, { rejectWithValue }) => {
    try {
      const result = await auditReview(id, data)
      return { id, data: result }
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

const auditSlice = createSlice({
  name: 'audit',
  initialState,
  reducers: {
    clearError: (state) => {
      state.error = null
    },
  },
  extraReducers: (builder) => {
    builder
      // 获取待审核笔记列表
      .addCase(fetchPendingNotes.pending, (state) => {
        state.loading = true
        state.error = null
      })
      .addCase(fetchPendingNotes.fulfilled, (state, action) => {
        state.loading = false
        state.noteList = action.payload.data || action.payload.list || []
        state.noteTotal = action.payload.total || 0
      })
      .addCase(fetchPendingNotes.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
      // 审核笔记
      .addCase(reviewNote.pending, (state) => {
        state.loading = true
      })
      .addCase(reviewNote.fulfilled, (state, action) => {
        state.loading = false
        // 可以选择从列表中移除已审核的笔记
        state.noteList = state.noteList.filter((item) => item.id !== action.payload.id)
      })
      .addCase(reviewNote.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
      // 获取待审核书评列表
      .addCase(fetchPendingReviews.pending, (state) => {
        state.loading = true
        state.error = null
      })
      .addCase(fetchPendingReviews.fulfilled, (state, action) => {
        state.loading = false
        state.reviewList = action.payload.data || action.payload.list || []
        state.reviewTotal = action.payload.total || 0
      })
      .addCase(fetchPendingReviews.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
      // 审核书评
      .addCase(reviewBookReview.pending, (state) => {
        state.loading = true
      })
      .addCase(reviewBookReview.fulfilled, (state, action) => {
        state.loading = false
        // 可以选择从列表中移除已审核的书评
        state.reviewList = state.reviewList.filter((item) => item.id !== action.payload.id)
      })
      .addCase(reviewBookReview.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
  },
})

export const { clearError } = auditSlice.actions
export default auditSlice.reducer

